package pxn;

import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.*;

public class Pz {
	public static Pz pz=new Pz();
	private Pz() {
		Shezhi.getShezhi();
	}
	public static Pz getPz() {
		return pz;
	}
	int conut = 1;
	public static int ADDRESS=0;
	public static int PORT=0;
	public static int MYSQLNAME=0;
	public static int USER=0;
	public static int PASSWORD=0;
	//0 没有KEY 1 有KEY没有Value  2 都有
	//下面是读取方法：
	//InputStream is = this.getClass().getResourceAsStream("a.properties");
	Properties p = new Properties();//在主方法中可以？？这里就不行？？
	public void getKey() {	
		try {
			InputStream in=new BufferedInputStream(new FileInputStream(System.getProperty("user.dir") + "\\res\\a.properties"));
			p.load(in);
			Iterator<String> iterator=p.stringPropertyNames().iterator();
			//Iterator??
			for (;iterator.hasNext();) {
				//获取后  要传递给链接MYsql的类
				String string=iterator.next();
				//System.out.println(string+p.getProperty(string));
				conut+=1;
				//使用switch 会不会好一点
				//System.out.println(string);
				if (string.equals("address")) {
					//string.equals("address")与"address".equals(string)  null
					ADDRESS=1;
					
					if (!p.getProperty(string).equals("")) {
						ADDRESS=2;
						Shezhi.ADDRESS=p.getProperty(string);
					}
				}else if (string.equals("port")) {
					PORT=1;
					
					if (!p.getProperty(string).equals("")) {
						PORT=2;
						Shezhi.PORT=p.getProperty(string);
					}
				}else if (string.equals("mysqlname")) {
					MYSQLNAME=1;
					
					if (!p.getProperty(string).equals("")) {
						MYSQLNAME=2;
						Shezhi.MYSQLNAME=p.getProperty(string);
					}
				}else if (string.equals("user")) {
					USER=1;
					
					if (!p.getProperty(string).equals("")) {
						USER=2;
						Shezhi.USER=p.getProperty(string);
					}
				}else if (string.equals("password")) {
					PASSWORD=1;
					
					if (!p.getProperty(string).equals("")) {
						PASSWORD=2;
						Shezhi.PASSWORD=p.getProperty(string);
					}
				}
			}
		
//		System.out.println(Shezhi.ADDRESS);
//		System.out.println(Shezhi.PORT);
//		System.out.println(Shezhi.MYSQLNAME);
//		System.out.println(Shezhi.USER);
//		System.out.println(Shezhi.PASSWORD);
		}
		catch(Exception e) {
			System.out.println(conut+"e");
		}
	}
	public void setKeya() {
		p.put("address", "");
	}
	public void setKeypt() {
		p.put("port", "");
	}
	public void setKeym() {
		p.put("mysqlname", "");
	}
	public void setKeyu() {
		p.put("user", "root");
	}
	public void setKeypw() {
		p.put("password", "123456");
	}
	public void setKeyD() {
		p.put("address", "qdm153527181.my3w.com");
		p.put("port", "3306");
		p.put("mysqlname", "qdm153527181_db");
		p.put("user", "qdm153527181");
		p.put("password", "6z687338");
	}
	public void setKey() {
		p.put("address", Shezhi.ADDRESS);
		p.put("port", Shezhi.PORT);
		p.put("mysqlname", Shezhi.MYSQLNAME);
		p.put("user", Shezhi.USER);
		p.put("password", Shezhi.PASSWORD);
	}
	public void set() {
		try {
			p.store(new FileOutputStream(System.getProperty("user.dir") + "\\res\\a.properties"), null);
		} catch (FileNotFoundException e) {
			// TODO 自动生成的 catch 块
			e.printStackTrace();
		} catch (IOException e) {
			// TODO 自动生成的 catch 块
			e.printStackTrace();
		}
	}
}
	//p.load(is);
	//System.out.println("ip = "+p.getPropertie("ip")+"; port = "+p.getPropertie("port"));
	//输出为ip = 192.168.1.254; port = 8080
	 
	//下面是写入
	//Properties p = new Properties();
	//p.put("netmask","255.255.254.0");
	//p.put("netgate","192.168.0.1");
	//p.put("ip","192.168.1.77");
//	try {
//	    p.store(new FileOutputStream("myTest.properties"), null);
//	    } catch (IOException ex) {}
	/*执行后
	myTest.properties内容变为：
	#Sun May 27 11:20:17 CST 2007
	port=8080
	netmask=255.255.254.0
	ip=192.168.1.77*/

